# -*- coding: utf-8 -*-

import jsonpickle

from common.logger import Logger
from mq.message_body import Message
from mq.queue import M_QUEUE, TOPICS

# lock = threading.Lock()
LOGGER = Logger(__name__)


class EventPublisher:
    """
    消息事件发布者
    """

    @staticmethod
    def publish(message=Message):
        """
        发布消息
        :param message:
        :return:
        """
        # LOGGER.info("发布消息：" + jsonpickle.dumps(message.__dict__))
        # 判断是否有订阅者
        if message.topic not in TOPICS:
            LOGGER.warning(f"主题 {message.topic} 没有订阅者，本条消息将被丢弃")
            return False
        # lock.acquire()
        M_QUEUE.append(message)
        # print(jsonpickle.dumps(M_QUEUE))
        # lock.release()
        return True


# 定义一个简易函数名方便调用
publish_event = EventPublisher.publish
